package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Viewpagolprofile extends Activity implements OnClickListener {

	Button back;
	Button edit;
	TextView txtstuID;
	TextView txtintakeID;
	TextView txtstuname;
	TextView txtemail;
	TextView txtcontactno;
	TextView txtprojtitle;
	TextView txtsupervisor;
	TextView txtadvisor;
	TextView txtPPFSubmission;
	TextView txtPSFSubmission;
	TextView txtmidpointinterview;
	TextView txtprojectsubmissiondate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewpagolprofile);

		back = (Button) findViewById(R.id.btnback);
		edit = (Button) findViewById(R.id.btneditprofile);

		txtstuID = (TextView) findViewById(R.id.txtPagolstuID);
		txtintakeID = (TextView) findViewById(R.id.txtPagolintakeID);
		txtstuname = (TextView) findViewById(R.id.txtstuname);
		txtemail = (TextView) findViewById(R.id.txtemail);
		txtcontactno = (TextView) findViewById(R.id.txtcontactno);
		txtprojtitle = (TextView) findViewById(R.id.txtprojtitle);
		txtsupervisor = (TextView) findViewById(R.id.txtsupervisor);
		txtadvisor = (TextView) findViewById(R.id.txtadvisor);
		txtPPFSubmission = (TextView) findViewById(R.id.txtPPFSubmission);
		txtPSFSubmission = (TextView) findViewById(R.id.txtPSFSubmission);
		txtmidpointinterview = (TextView) findViewById(R.id.txtmidpointinterview);
		txtprojectsubmissiondate = (TextView) findViewById(R.id.txtprojectsubmissiondate);

		Intent intename = getIntent();
		String studID = intename.getStringExtra("studID");
		String intakeID = intename.getStringExtra("intakeID");
		String studName = intename.getStringExtra("studName");
		String email = intename.getStringExtra("email");
		String contactNo = intename.getStringExtra("contactNo");
		String pojTitle = intename.getStringExtra("projTitle");
		String supervisor = intename.getStringExtra("supervisor");
		String advisor = intename.getStringExtra("advisor");
		String ppf = intename.getStringExtra("ppfDate");
		String psf = intename.getStringExtra("psfDate");
		String mid = intename.getStringExtra("midDate");
		String submission = intename.getStringExtra("subDate");

		txtstuID.setText(studID);
		txtintakeID.setText(intakeID);
		txtstuname.setText(studName);
		txtemail.setText(email);
		txtcontactno.setText(contactNo);
		txtprojtitle.setText(pojTitle);
		txtsupervisor.setText(supervisor);
		txtadvisor.setText(advisor);
		txtPPFSubmission.setText(ppf);
		txtPSFSubmission.setText(psf);
		txtmidpointinterview.setText(mid);
		txtprojectsubmissiondate.setText(submission);

		back.setOnClickListener(this);
		edit.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.viewpagolprofile, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == back) {
			Intent i = new Intent(this, Pagol.class);
			startActivity(i);
		}
		if (v == edit) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/student_pp.asp?profilePID=edtprofile");

			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("title", "//title---");
			xpathMap.put("studentID","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[3]---");
			xpathMap.put("intakeID","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[3]/td[3]---");
			xpathMap.put("studentName","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[3]---");
			///html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[6]/td[3]/input---value 
			xpathMap.put("email","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[5]/td[3]/input---value");
			//xpathMap.put("contactNo","html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[6]/td[3]/input---value");
			xpathMap.put("proTitle","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[7]/td[3]---");
			xpathMap.put("supervisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[8]/td[3]---");
			xpathMap.put("advisor","/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[9]/td[3]---");

			request.setXpathMap(xpathMap);

			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);

			request.setType("GET");

			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String intake = respones.getResultsMap().get("intakeID").trim();
			String intakeArray [] = intake.split("&");
			String intakeID = intakeArray[0];
			
			Intent i = new Intent(this, UpdatePagolProfile.class);
			 i.putExtra("title",respones.getResultsMap().get("title").trim());
			 i.putExtra("studentID",respones.getResultsMap().get("studentID").trim());
			 i.putExtra("intakeID",intakeID);
			 i.putExtra("studentName", respones.getResultsMap().get("studentName").trim());
			 i.putExtra("email", respones.getResultsMap().get("email").trim());
			 //i.putExtra("contactNo",respones.getResultsMap().get("contactNo").trim());
			 i.putExtra("proTitle",respones.getResultsMap().get("proTitle").trim());
			 i.putExtra("supervisor",respones.getResultsMap().get("supervisor").trim());
			 i.putExtra("advisor", respones.getResultsMap().get("advisor").trim());
			//
			Toast.makeText(getApplicationContext(),
			respones.getResultsMap().get("email").trim(),
			Toast.LENGTH_LONG).show();
			startActivity(i);
		}
	}

}
